package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.entity.vo.CategoryLevel2Vo;
import com.atguigu.gulimall.product.service.CategoryService;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-01-04-9:11
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {
        //查出所有的1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", categoryEntities);

        return "index";
    }

    /**
     * 获取三级分类菜单
     *
     * @author 胡菜鸡
     * @date 2022/1/6 14:15
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<CategoryLevel2Vo>> getCatalogJson() {
        Map<String, List<CategoryLevel2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        //1、获取一把锁（锁名相同就是同一把锁）
        RLock lock = redissonClient.getLock("my-lock");

        //2、加锁
        lock.lock();        //阻塞式等待（相当于while）
//        lock.lock(10, TimeUnit.SECONDS);    //10s后自动解锁（不会自动续期）
        try {
            System.out.println("加锁成功，执行业务" + Thread.currentThread().getId());
            Thread.sleep(5000);
        } catch (Exception e) {

        } finally {
            //3、解锁
            lock.unlock();
            System.out.println("释放锁..." + Thread.currentThread().getId());
        }

        return "hello";
    }

    /**
     * 读写锁
     * @author 胡菜鸡
     * @date 2022/1/7 13:41
     */
    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = lock.writeLock();     //获取写锁
        String s = "";
        try {
            rLock.lock();   //上锁
            s = UUID.randomUUID().toString();
            Thread.sleep(10000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }
    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = lock.readLock();  //获取读锁
        rLock.lock();   //上锁
        String s = "";
        try {
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }

}
